var utils = require('../../../../utils/util.js')
var c = require('../../../../utils/config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    title: '',
    name: '',
    phone: '',
    cat_title: '',
    address: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loaddata();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 加载数据
   */
  loaddata: function () {
    var that = this;
    var token = wx.getStorageSync("userinfo").wap_token;
    var data = {
      url: "/zfshop/shopapply/details",
      token: token,
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        var data = res.data;
        if (data != ""){
          that.setData({
            id: data.id,
            title: data.title,
            name: data.name,
            phone: data.phone,
            cat_title: data.cat_title,
            address: data.address,
          });
        }
      } else {

      }
    });
  },
  /**
   * 编辑厂家名称
   */
  edittitle: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  /**
   * 编辑姓名
   */
  editname: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  /**
   * 编辑电话
   */
  editphone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  /**
   * 编辑分类
   */
  editcat_title: function (e) {
    this.setData({
      cat_title: e.detail.value
    })
  },
  /**
   * 编辑地址
   */
  editaddress: function (e) {
    this.setData({
      address: e.detail.value
    })
  },
  submit: function () {
    var id = this.data.id;
    var title = this.data.title;
    var name = this.data.name;
    var phone = this.data.phone;
    var cat_title = this.data.cat_title;
    var address = this.data.address;
    if (title == "") {
      wx.showToast({
        title: '请输入厂家姓名',
        icon: "none"
      })
      return false;
    };
    if (name == "") {
      wx.showToast({
        title: '请输入负责人姓名',
        icon: "none"
      })
      return false;
    };
    if (phone == "") {
      wx.showToast({
        title: '请输入负责人电话',
        icon: "none"
      })
      return false;
    };
    if (cat_title == "") {
      wx.showToast({
        title: '请选输入产品类别',
        icon: "none"
      })
      return false;
    }
    if (address == "") {
      wx.showToast({
        title: '请填写详细的厂家地址',
        icon: "none"
      })
      return false;
    }

    var that = this;
    var token = wx.getStorageSync("userinfo").wap_token;
    if (id == "") {
      var data = {
        url: "/zfshop/shopapply/add",
        token: token,
        title: title,
        name: name,
        phone: phone,
        cat_title: cat_title,
        address: address,
      };
    } else {
      var data = {
        id: id,
        url: "/zfshop/shopapply/edit",
        token: token,
        title: title,
        name: name,
        phone: phone,
        cat_title: cat_title,
        address: address,
      };
    };
    
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        wx.showToast({
          title: "申请提交成功",
          icon: "success"
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 2000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
    });
  }

})